package com.shengde.jinwawaforleader.model.myJob.instance_message;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.exceptions.HyphenateException;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;

import java.util.List;

import static com.shengde.jinwawaforleader.HomeApplication.showToastLong;
import static com.shengde.jinwawaforleader.HomeApplication.showToastShort;


/**
 * Created by Administrator on 2016/10/18 0018.
 */
public class GroupActivity extends com.shengde.jinwawaforleader.base.BaseActivity implements AdapterView.OnItemClickListener {
	private static final String TAG = "GroupActivity";
	private static final int FINSH = 1;
	private TextView tv_back;
	private TextView tv_title;
	private LinearLayout loading_main;
	private LinearLayout loading_no_recourse;
	private ListView listView;
	private List<EMGroup> grouplist;  //群组联系人
	private CommonBaseAdapter mGroupListAdapter;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (mGroupListAdapter == null) {
						mGroupListAdapter = new CommonBaseAdapter(GroupActivity.this, grouplist, R.layout.link_group_list_item) {

							@Override
							public void convert(ViewHolder holder, int position) {
								holder.setText(R.id.tv_name, grouplist.get(position).getGroupName());
							}
						};
						listView.setAdapter(mGroupListAdapter);
						loading_main.setVisibility(View.GONE);
					} else {
						mGroupListAdapter.notifyDataSetChanged();
					}
					break;
				case 2:
					showToastLong(getString(R.string.Failed_to_get_group_chat_information));
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.group_list_activity);

	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		loading_main = (LinearLayout) findViewById(R.id.loading_main);
		loading_no_recourse = (LinearLayout) findViewById(R.id.loading_no_recourse);
		listView = (ListView) findViewById(R.id.lv_grouplist);

	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.group_list));
		showLinkgroupList();

	}

	/**
	 * 获取群组列表
	 */
	private void showLinkgroupList() {
		new Thread() {
			@Override
			public void run() {
				try {
					grouplist = EMClient.getInstance().groupManager().getJoinedGroupsFromServer();
					Log.i(TAG, "run: groupList" + grouplist.size());
					mHandler.sendEmptyMessage(FINSH);
				} catch (HyphenateException e) {
					e.printStackTrace();
					mHandler.sendEmptyMessage(2);
				}
			}
		}.start();
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		listView.setOnItemClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(GroupActivity.this, ChatActivity.class);
		// it is group chat
		intent.putExtra("chatType", Constant.CHATTYPE_GROUP);
		intent.putExtra("userId", grouplist.get(position).getGroupId());
		startActivity(intent);
	}
}
